package com.bosssoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.po.FibIncomeBudgetPO;
import com.bosssoft.entity.vo.FibIncomeBudgetVO;
import com.bosssoft.feign.DepartmentFeignService;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibIncomeBudgetMapper;
import com.bosssoft.service.FibIncomeBudgetService;

import javax.annotation.Resource;

/**
 * @author Li kexi
 * Created on 2021/8/19.15:46
 */
@Service
public class FibIncomeBudgetServiceImpl extends ServiceImpl<FibIncomeBudgetMapper, FibIncomeBudgetPO> implements FibIncomeBudgetService {

    @Resource
    private FibIncomeBudgetMapper fibIncomeBudgetMapper;

    @Resource
    private DepartmentFeignService departmentFeignService;

    @Override
    public int updateBatch(List<FibIncomeBudgetPO> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<FibIncomeBudgetPO> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<FibIncomeBudgetPO> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public int insertOrUpdate(FibIncomeBudgetPO record) {
        return baseMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(FibIncomeBudgetPO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Override
    public Long updateByDetail(Long deptId, Integer year, String incomeType, BigDecimal amount) {

        FibIncomeBudgetPO fibIncomeBudgetPO = fibIncomeBudgetMapper.selectByDeptIdAndYear(deptId,year);
        if (fibIncomeBudgetPO!=null){
            FibIncomeBudgetPO budgetPOResult = changeAmount(fibIncomeBudgetPO, incomeType, amount);
            fibIncomeBudgetMapper.updateById(budgetPOResult);
            Long id = budgetPOResult.getId();
            return id;
        }else{
            FibIncomeBudgetPO budgetPO = new FibIncomeBudgetPO();
            FibIncomeBudgetPO budgetPOResult = changeAmount(budgetPO, incomeType, amount);
            baseMapper.insert(budgetPOResult);
            Long id = budgetPOResult.getId();
            //System.out.println(budgetPOResult.getId());
            return id;
        }
    }

    public FibIncomeBudgetPO changeAmount(FibIncomeBudgetPO fibIncomeBudgetPO, String incomeType,BigDecimal amount){
        if (incomeType.equals("admin_strative")) {
            fibIncomeBudgetPO.setAdministrativeFee(fibIncomeBudgetPO.getAdministrativeFee().add(amount));
        }
        if (incomeType.equals("punish_confiscate")) {
            fibIncomeBudgetPO.setPunishConfiscateFee(fibIncomeBudgetPO.getPunishConfiscateFee().add(amount));
        }
        if (incomeType.equals("special")) {
            fibIncomeBudgetPO.setSpecialFee(fibIncomeBudgetPO.getSpecialFee().add(amount));
        }
        return fibIncomeBudgetPO;
    }

    @Override
    public int changeStatus(Long deptId, Integer year, Integer nextStatus) {
        int i = fibIncomeBudgetMapper.changeStatus(deptId, year, nextStatus);
        return i;
    }

    @Override
    public FibIncomeBudgetVO selectIncomeBudgetReport(String deptName, Integer year) {
        FibIncomeBudgetPO budgetPO = fibIncomeBudgetMapper.selectByDeptNameAndYear(deptName, year);
        FibIncomeBudgetVO budgetVO = new FibIncomeBudgetVO();
        BeanUtils.copyProperties(budgetPO, budgetVO);
        return budgetVO;
    }

    @Override
    public Map<String, BigDecimal> incomeSheet(Integer status, Integer year) {

        List<BigDecimal[]> feeLists = fibIncomeBudgetMapper.selectByStatus(status, year);
        Map<String, BigDecimal> map = new HashMap<>();
        //行政事业性收费
        BigDecimal administrativeFee = new BigDecimal(0);
        //罚没收入
        BigDecimal punishConfiscateFee = new BigDecimal(0);
        //专项收入
        BigDecimal specialFee = new BigDecimal(0);
        for (BigDecimal[] feeList : feeLists) {
            administrativeFee = administrativeFee.add(feeList[0]);
            punishConfiscateFee = punishConfiscateFee.add(feeList[1]);
            specialFee = specialFee.add(feeList[2]);
        }
        map.put("administrative_fee", administrativeFee);
        map.put("punish_confiscate_fee", punishConfiscateFee);
        map.put("special_fee", specialFee);
        return map;
    }

    @Override
    public Page<FibIncomeBudgetVO> queryAllIncome(Integer current, Integer size) {

        Page<FibIncomeBudgetPO> page = new Page<>(current, size);
        Page<FibIncomeBudgetPO> poPage = fibIncomeBudgetMapper.selectPage(page, null);
        Page<FibIncomeBudgetVO> voPage = new Page<>(current, size);
        BeanUtils.copyProperties(poPage,voPage);
        return voPage;
    }

    @Override
    public List<Page<FibIncomeBudgetPO>> queryMyIncomeAndChildren(Integer current, Integer size,Long deptId) {

        // ids 为当前部门所有子部门的id
        List<Long> ids = departmentFeignService.selectChildrenIds(deptId);

        Page<FibIncomeBudgetPO> poPage = new Page<>(current, size);
        QueryWrapper<FibIncomeBudgetPO> wrapper = new QueryWrapper<>();
        List<Page<FibIncomeBudgetPO>> pageList = new ArrayList<>();
        List<FibIncomeBudgetPO> pos = new ArrayList<>();
        for(Long id:ids){
            Page<FibIncomeBudgetPO> page = fibIncomeBudgetMapper.selectPage(poPage, wrapper);
            pageList.add(page);
        }
        return pageList;
    }
}


